import {Component} from '@angular/core';
import {IonicPage, NavController} from 'ionic-angular';

import {KitProvider} from "../../../core/foundations/KitProvider";
import {CartProvider} from "../../../providers/cartProvider";
import {HomePage} from "../home/home";
/**
 * Generated class for the AlertPage page.
 *
 * See http://ionicframework.com/docs/components/#navigation for more info
 * on Ionic pages and navigation.
 */
@IonicPage()
@Component({
  selector: 'page-alert',
  templateUrl: 'alert.html',
})
export class CustomerAlertPage {
  stores: any;
  tables: any;
  Store_ID: string;
  Table_ID: string;
  StoreId: { title: string };
  TableId: { title: string };


  constructor(public navCtrl: NavController,
              public cart: CartProvider,
              public kit: KitProvider) {
    this.StoreId = {title: 'Select Store ID'};
    this.TableId = {title: 'Select Table ID'};
  }

  ionViewDidLoad() {
    this.cart.getTables().subscribe(Stores => {
      this.stores = Stores;
      this.Store_ID = this.cart.Store_ID();
      this.Table_ID = this.cart.Table_ID();
     /* if (this.Store_ID != null)
        this.setTables(this.Store_ID);*/
      console.log(this.stores);
    });

  }

  storeid():string{
    let storeid= localStorage.getItem('Store_ID');
    if(storeid!= null)
      return storeid;
    return 'all';
  }

  setTables(store_id: string) {
    for (let s of this.stores) {
      if (s.Store_ID == store_id)
        this.tables = s.Tables;
    }
  }

  changeStoreId(store_id) {
    this.Table_ID = null;
    this.setTables(store_id);
  }

  save() {
    if (this.Store_ID == null || this.Table_ID == null || this.Store_ID == '' || this.Table_ID == '') {
      this.kit.alertMsg('Error', 'Please select Store ID and Table ID!');
      return;
    } else {
      this.cart.Store_ID(this.Store_ID);
      this.cart.Table_ID(this.Table_ID);
      this.navCtrl.setRoot(HomePage);
    }
  }

}
